CROSS_COMPILE	= 

CC			= $(CROSS_COMPILE)gcc
LD			= $(CROSS_COMPILE)gcc
SED			= sed

TARGET			= echo echod
SRCDIRS			= .
EXCLUDE_FILES	= %/echo.c %/echod.c
INCLUDES		= 
LIBS			= 
STATICLIBS		= 

CFLAGS			+= 
LDFLAGS			+= 
DFLAGS			+= -MM

CFLAGS		:= $(CFLAGS) $(INCLUDES) $(addprefix -I, $(SRCDIRS)) -pipe -Wall -Wextra
LDFLAGS		:= $(LDFLAGS) $(LIBS)

SRCS		= $(filter-out $(EXCLUDE_FILES), $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c)))
OBJS		= $(SRCS:.c=.o)
DEPENDS		= $(SRCS:.c=.d)

all: $(TARGET)

echod: $(OBJS) echod.o
	@echo LD $@
	@$(LD) -o $@ $(filter %.o ,$+) $(LDFLAGS)

echo: $(OBJS) echo.o
	@echo LD $@
	@$(LD) -o $@ $(filter %.o ,$+) $(LDFLAGS)

%.o : %.c
	@echo CC $@
	@$(CC) -c $(CFLAGS) $< -o $@

%.d: %.c
	@echo GEN DEPENDENT $@
	@$(CC) $(DFLAGS) $(CFLAGS) $< | $(SED) 's,$(basename $(notdir $<))\.o\s*:, $(basename $<).o: ,g' > $@

sinclude $(DEPENDS)

clean:
	$(RM) $(OBJS) $(DEPENDS) echo.o echod.o

distclean: clean
	$(RM) $(TARGET)
